import { fileURLToPath, URL } from 'node:url'

import { defineConfig, UserConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'
import { viteMockServe } from "vite-plugin-mock"

/**
 * 通用vite配置
 */
export default defineConfig(({ mode }): UserConfig => {
    return {
        plugins: [
            vue(),
            vueDevTools(),
            // Elementplus自动引入插件
            AutoImport({
                resolvers: [ElementPlusResolver()],
            }),
            Components({
                resolvers: [ElementPlusResolver()],
            }),
            // svg图标使用到的插件
            createSvgIconsPlugin({
                iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
                symbolId: 'icon-[dir]-[name]',
            }),
            viteMockServe({
                mockPath: './src/mock',   // mock文件存放目录
                enable: true,             // 是否启用mock功能
                logger: true              // 控制台是否显示请求日志
            }),
        ],
        resolve: {
            alias: {
                '@': fileURLToPath(new URL('./src', import.meta.url))
            },
        },
        css: {
            // 定义全局css变量，到时候这里面定义的变量，所有vue文件中都可以使用
            preprocessorOptions: {
                scss: {
                    additionalData: `@use "@/assets/styles/variables.scss" as *;`
                }
            }
        }
    }
})
